/**
 * @author [siwi]
 * @email [siwi@siwi.me]
 * @create date 2017-11-11 09:25:38
 * @modify date 2017-11-11 09:25:38
 * @desc [api 接口逻辑]
*/


/** Util && config **/
const Util = require('../lib/Util')
const Redis = require('../lib/redis').connect
class BootApi {
    constructor() {

    }
    async gateway (data) {
        switch (data['method']) {
            case 'test.test':
                return await this.test(data)
                break;
            case 'bootapi.getlibrarieslist':
                return await this.librarieslist(data)
                break;
            case 'bootapi.getlibrariesdetail':
                return await this.librariesdetail(data)
                break;
            default:
            console.log(data)
                return {
                    error_code: 'NOT_EXIST_METHOD',
                    error_msg: '不存在的接口方法'
                }
                break;
        }
    }
    async librarieslist (data) {
        let res = await Redis.hgetall('BOOT_API_LIST_SET')
        console.log(res)
        let rowset = []
        for (let item in res) {
            rowset.push(JSON.parse(res[item]))
        }
        return {
            ret: 0,
            data: {
                rowset: rowset
            }
        }
    }
    async librariesdetail (data) {
        let require = ['name'] 
        let check = await Util.check_require(require, data)
        if (check) {
            return {
                ret: -1,
                msg: check
            }
        }
        let res = await Redis.hget('BOOT_API_DETAIL_SET', data['name'])
        return {
            ret: 0,
            data: JSON.parse(res)
        }
    }
    
}

module.exports = new BootApi()